package com.udf.hive.udf.disturb;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.logging.Logger;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

/**
 * 日期向上偏移取整
 * 
 * @author Luyf
 * @version [版本号, 2021年2月18日]
 */
public class OffsetPlusDateUDF extends UDF
{
    private static Logger logger = Logger.getLogger("OffsetPlusDateUDF");

    public Text evaluate(Text data, String paramValue) {
        if (data != null) {
            String str = data.toString();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateTwo = null;
            try {
                Date dateOne = sdf.parse(str);
                Instant ins = dateOne.toInstant();
                dateOne = Date.from(ins.plusSeconds(Integer.parseInt(paramValue)));
                dateTwo = sdf.format(dateOne);
            }
            catch (ParseException e) {
                e.printStackTrace();
            }
            return new Text(dateTwo);
        }
        else {
            return null;
        }
    }

    public static void main(String[] args) {
        logger.info(String.valueOf(new OffsetPlusDateUDF().evaluate(new Text("2021-02-19 16:47:09"), "5")));
    }
}
